package com.sxt.pro.controller;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxt.pro.pojo.LivePay;
import com.sxt.pro.service.LivePayService;
import com.sxt.pro.vo.LivePayVo;
import com.sxt.sys.common.AppFileUtils;
import com.sxt.sys.common.Constast;
import com.sxt.sys.common.DataGridView;
import com.sxt.sys.common.ResultObj;
import com.sxt.sys.common.WebUtils;


/**  

* @Description: 生活缴费控制层

* @author xinxiaokang  

* @date 2021年3月15日 下午4:23:49

*/
@RestController
@RequestMapping("/livepay")
public class LivePayController {


	@Autowired
	private LivePayService livePayService;
	
	/**
	 * 查询生活缴费分页列表
	 * @param livePayVo
	 * @return
	 */
	@RequestMapping("/loadAllLivePay")
	public DataGridView loadAllLivePay(LivePayVo livePayVo){
		//存入网点编号和用户名放入session中
		 int banknbr = (int) WebUtils.getSession().getAttribute("banknbr");
		 livePayVo.setBanknbr(banknbr);
		//初始化 当前页、每页显示条数
		IPage<LivePay> page = new Page<>(livePayVo.getPage(), livePayVo.getLimit());
		//封装对象查询条件
		QueryWrapper<LivePay> queryWrapper = new QueryWrapper<LivePay>();
		//筛选条件：网点编号
		  queryWrapper.eq(livePayVo.getBanknbr()!=null&&livePayVo.getBanknbr()!=0,"banknbr",livePayVo.getBanknbr());
		//筛选条件:名称
		queryWrapper.like(StringUtils.isNotBlank(livePayVo.getLivepayname()), "livepayname", livePayVo.getLivepayname());
		//筛选条件：大于开始时间
		queryWrapper.ge(livePayVo.getStartTime()!=null, "createtime", livePayVo.getStartTime());
		//筛选条件：小于结束时间
		queryWrapper.le(livePayVo.getEndTime()!=null, "createtime", livePayVo.getEndTime());
		queryWrapper.orderByDesc("createtime");
		//获取分页数据
		this.livePayService.page(page, queryWrapper);
		return new DataGridView(page.getTotal(), page.getRecords());
	}
	
	/**
	 * 添加生活缴费
	 * @param livePayVo
	 * @return
	 */
	@RequestMapping("addLivePay")
	public ResultObj addLivePay(LivePayVo livePayVo) {
		//添加时，网点默认
		int banknbr = (int) WebUtils.getSession().getAttribute("banknbr");
		livePayVo.setBanknbr(banknbr);
		try {
			if(livePayVo.getFlowimage()!=null&&livePayVo.getFlowimage().endsWith("_temp")) {
				//流程图片名称
				String newName= AppFileUtils.renameFile(livePayVo.getFlowimage());
				livePayVo.setFlowimage(newName);
				livePayVo.setCreatetime(new Date());
			}
			if(livePayVo.getPayimage()!=null&&livePayVo.getPayimage().endsWith("_temp")) {
				//支付图片名称
				String newName=  AppFileUtils.renameFile(livePayVo.getPayimage());
				livePayVo.setPayimage(newName);
			}
			if(livePayVo.getMarkimage()!=null&&livePayVo.getMarkimage().endsWith("_temp")) {
				//背景图片名称
				String newName=AppFileUtils.renameFile(livePayVo.getMarkimage());
				livePayVo.setMarkimage(newName);
			}
			this.livePayService.save(livePayVo);
			return ResultObj.ADD_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.ADD_ERROR;
		}
	}

	/**
	 * 修改生活缴费
	 * @param livePayVo
	 * @return
	 */
	@RequestMapping("updateLivePay")
	public ResultObj updateLivePay(LivePayVo livePayVo) {
		try {
			//说明是不默认图片
			if(!(livePayVo.getFlowimage()!=null&&livePayVo.getFlowimage().equals(Constast.IMAGES_DEFAULTGOODSIMG_PNG))) {
				if(livePayVo.getFlowimage().endsWith("_temp")) {
					String newName= AppFileUtils.renameFile(livePayVo.getFlowimage());
					livePayVo.setFlowimage(newName);
					//删除原先的图片
					String oldPath=this.livePayService.getById(livePayVo.getId()).getFlowimage();
					if (!(oldPath.equals(Constast.IMAGES_DEFAULTGOODSIMG_PNG))) {
						AppFileUtils.removeFileByPath(oldPath);
					}
				}
			}
			if(!(livePayVo.getPayimage()!=null&&livePayVo.getPayimage().equals(Constast.IMAGES_DEFAULTGOODSIMG_PNG))) {
				if(livePayVo.getPayimage().endsWith("_temp")) {
					String newName= AppFileUtils.renameFile(livePayVo.getPayimage());
					livePayVo.setPayimage(newName);
					//删除原先的图片
					String oldPath=this.livePayService.getById(livePayVo.getId()).getPayimage();
					if (!(oldPath.equals(Constast.IMAGES_DEFAULTGOODSIMG_PNG))) {
					AppFileUtils.removeFileByPath(oldPath);
					}
				}
			}
			if(!(livePayVo.getMarkimage()!=null&&livePayVo.getMarkimage().equals(Constast.IMAGES_DEFAULTGOODSIMG_PNG))) {
				if(livePayVo.getMarkimage().endsWith("_temp")) {
					String newName= AppFileUtils.renameFile(livePayVo.getMarkimage());
					livePayVo.setMarkimage(newName);
					//删除原先的图片
					String oldPath=this.livePayService.getById(livePayVo.getId()).getMarkimage();
					if (!(oldPath.equals(Constast.IMAGES_DEFAULTGOODSIMG_PNG))) {
						AppFileUtils.removeFileByPath(oldPath);
					}
				}
			}
			this.livePayService.updateById(livePayVo);
			return ResultObj.UPDATE_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.UPDATE_ERROR;
		}
	}

	/**
	 * 删除生活缴费
	 * @param id
	 * @param flowimage
	 * @param payimage
	 * @param markimage
	 * @return
	 */
	@RequestMapping("deleteLivePay")
	public ResultObj deleteLivePay(Integer id,String flowimage,String payimage,String markimage) {
		try {
			//删除原文件
			if (!flowimage.isEmpty() && !flowimage.equals("images/defaultgoodsimg.png")) {
				AppFileUtils.removeFileByPath(flowimage);
			}
			if (!payimage.isEmpty() && !payimage.equals("images/defaultgoodsimg.png")) {
				AppFileUtils.removeFileByPath(payimage);
			}
			if (!markimage.isEmpty() && !markimage.equals("images/defaultgoodsimg.png")) {
				AppFileUtils.removeFileByPath(markimage);
			}
			this.livePayService.removeById(id);
			return ResultObj.DELETE_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.DELETE_ERROR;
		}
	}
	
}
